﻿using System;
using System.Collections.Generic;
using System.Text;

//CID 0797 //2011.02.25 //Begin
namespace InfoJetSoft.Service.XPath.Exprs
{
    class BooleanFunctions
    {
        //CID 0797 //2011.02.25 //Begin
        public static bool boolean(object value)
        {
            if (value is double)
            {
                double doubleValue = (double)value;
                if (doubleValue == 0.0 || double.IsNaN(doubleValue))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else if (value is bool)
            {
                return (bool)value;
            }
            else if (value is string)
            {
                string stringValue = (string)value;
                if (stringValue.Length > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else if (value is INodeList)
            {
                INodeList nodeList = (INodeList)value;
                if (nodeList.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                throw new ExprException("Unsupported data type.");
            }
        }
        //CID 0797 //2011.02.25 //End

        //CID 0796 //2011.02.26 //Begin
        public static bool not(object value)
        {
            bool result = BooleanFunctions.boolean(value);
            return !result;
        }
        //CID 0796 //2011.02.26 //End

        //CID 0796 //2011.02.26 //Begin
        public static bool trueFunc()
        {
            return true;
        }
        //CID 0796 //2011.02.26 //End

        //CID 0796 //2011.02.26 //Begin
        public static bool falseFunc()
        {
            return false;
        }
        //CID 0796 //2011.02.26 //End

        //CID 0796 //2011.02.26 //Begin
        public static bool lang(string language)
        {
            throw new ExprException("lang() is not implemented.");
        }
        //CID 0796 //2011.02.26 //End

        //CID 0796 //2011.02.26 //Begin
        public static object CallFunction(string funcName, List<object> args)
        {
            if (funcName.Equals("boolean"))
            {
                if (args.Count == 1)
                {
                    return BooleanFunctions.boolean(args[0]);
                }
                else
                {
                    throw new ExprException("Wrong parameters in boolean().");
                }
            }
            else if (funcName.Equals("not"))
            {
                if (args.Count == 1)
                {
                    return BooleanFunctions.not(args[0]);
                }
                else
                {
                    throw new ExprException("Wrong parameters in not().");
                }
            }
            else if (funcName.Equals("true"))
            {
                if (args.Count == 0)
                {
                    return BooleanFunctions.trueFunc();
                }
                else
                {
                    throw new ExprException("Wrong parameters in true().");
                }
            }
            else if (funcName.Equals("false"))
            {
                if (args.Count == 0)
                {
                    return BooleanFunctions.falseFunc();
                }
                else
                {
                    throw new ExprException("Wrong parameters in false().");
                }
            }
            else if (funcName.Equals("lang"))
            {
                if (args.Count == 1 && args[0] is string)
                {
                    return BooleanFunctions.lang((string)args[0]);
                }
                else
                {
                    throw new ExprException("Wrong parameters in lang().");
                }
            }
            else
            {
                return null;
            }
        }
        //CID 0796 //2011.02.26 //End
    }
}
